package com.byd.modules.biz.service.impl;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.byd.modules.biz.dao.UserDictDao;
import com.byd.modules.biz.service.UserDictService;

@Service("userDictService")
public class UserDictServiceImpl implements UserDictService {
	
	@Autowired
	private UserDictDao userDictDao;
	@Override
	public void saveName(String name) {
		
		userDictDao.deleteWord(name);
		userDictDao.saveWord(name);		
	}

	@Override
	public void saveNickName(String nickName) {
		
		String[] wordArray = nickName.split(";");
		String[] array=unique(wordArray);
		userDictDao.deleteBatch(array);
		userDictDao.saveBatch(array);	
	}
	
	//去重
	private String[] unique(String[] arr) {

		Set set = new HashSet();
		// 遍历数组并存入集合,如果元素已存在则不会重复存入
		for (int i = 0; i < arr.length; i++) {

			if (StringUtils.isNotBlank(arr[i])) {
				set.add(arr[i]);
			}
		}
		return (String[])set.toArray(new String[set.size()]);  
	}
}
